import { utils } from "../../utils/CommonUtil";
import { LevelDataConst } from "../../constant/LevelDataConst";

export class LevelData {
    /** 金币解锁存本地 key*/
    public static UNLOCK_EYE_LEVEL: string = "UNLOCK_EYE_LEVEL";

    /** id*/
    public id: number;
    /** 关卡数*/
    public levelNum: number;
    /** 难度*/
    public difficult: number;
    /** 关卡场景名字*/
    public levelScene: string;
    /** 解锁类型 1--直接开放的  2--金币*/
    public unlockType: number;
    /** 解锁价格*/
    public unlockPrice: number;

    constructor(obj: string) {
        this.id = obj['id'];
        this.levelNum = obj['levelNum'];
        this.difficult = obj['difficult'];
        this.levelScene = obj['levelScene'];
        this.unlockType = obj['unlockType'];
        this.unlockPrice = obj['unlockPrice'];
    }

    /** 是否花钱解锁了*/
    public get IsUnlockEyeLevel(): boolean {
        return window.gameFacade.SDKMng.SDK.GetLocalData(LevelData.UNLOCK_EYE_LEVEL + this.id) == "1";
    }

    public set IsUnlockEyeLevel(value: boolean) {
        window.gameFacade.SDKMng.SDK.SaveLocalData(LevelData.UNLOCK_EYE_LEVEL + this.id, value ? "1" : "0");
    }

    /** 关卡是否通过了*/
    public get IsHavePass(): boolean {
        return window.gameFacade.SDKMng.SDK.GetLocalData(LevelDataConst.IS_LEVEL_PASS + this.id) == "1";
    }

    public set IsHavePass(value: boolean) {
        window.gameFacade.SDKMng.SDK.SaveLocalData(LevelDataConst.IS_LEVEL_PASS + this.id, value ? "1" : "0");
    }

    /** 当前关卡是否可以玩 （前面一关必须通过了 需要花费解锁的关卡 及时前一关通过了  还要花钱解锁）*/
    public IsCanPlay(): boolean {
        /** 如果是金币解锁*/
        if (this.unlockType == 2) {
            return this.IsUnlockEyeLevel;
        }
        else {
            return this.IsLastPass();
        }
    }
    /** 当前关卡上一关是否通过了*/
    public IsLastPass(): boolean {
        let lastLevelData = window.gameFacade.ConfigMng.GetLevelDataByLevelNum(this.levelNum - 1);
        return lastLevelData == null || lastLevelData.IsHavePass;
    }
    /** 关卡是否解锁*/
    public IsLevelUnLocked(): boolean {
        /** 直接开放*/
        if (this.unlockType == LevelDataConst.LEVLE_OPEN_TYPE) {
            return true;
        }
        else {
            return window.gameFacade.SDKMng.SDK.GetLocalData(LevelDataConst.IS_LEVEL_UNLOCKED) == "1";
        }
    }
    /** 返回玩家本关获得的最多的纪念品数*/
    public GetCurMostMemorial(): number {
        return parseInt(window.gameFacade.SDKMng.SDK.GetLocalData(LevelDataConst.LEVEL_MOST_MEMORIAL_NUM + this.id, "0"));
    }

    /** 根据当前最大纪念品 设置当前的最多纪念品数*/
    public SetCurMostMemorial(memorialNum: number) {
        let curNum = this.GetCurMostMemorial();
        if (curNum < memorialNum) {
            window.gameFacade.SDKMng.SDK.SaveLocalData(LevelDataConst.LEVEL_MOST_MEMORIAL_NUM + this.id, memorialNum.toString());
        }
    }
    /** 获得本关卡的最快速度记录str*/
    public GetMostSpeed(): string {
        let mostSpeed = parseFloat(window.gameFacade.SDKMng.SDK.GetLocalData(LevelDataConst.LEVEL_MOST_SPEED + this.id, "0"));

        if (mostSpeed == 0) {
            return "--:--:--";
        }
        else {

            return utils.CommonUtils.GetMillionSecondStr(mostSpeed);
        }
    }

    /** 更新最快速度*/
    public UpdateMostSpeed(time: number) {
        let mostSpeed = parseFloat(window.gameFacade.SDKMng.SDK.GetLocalData(LevelDataConst.LEVEL_MOST_SPEED + this.id, "0"));

        if (time <= mostSpeed || mostSpeed == 0) {
            window.gameFacade.SDKMng.SDK.SaveLocalData(LevelDataConst.LEVEL_MOST_SPEED + this.id, time.toString());
        }
    }
}